var gulp = require('gulp');
var path = require('path');
var fs = require('fs');
var clean = require('gulp-rimraf');
var less = require('gulp-less');
var minifyCSS = require('gulp-minify-css');
var LessPluginCleanCSS = require('less-plugin-clean-css');
var cleancss = new LessPluginCleanCSS({
    advanced: true
});
var ejs = require('gulp-ejs');
var htmlmin = require('gulp-htmlmin');
var rename = require('gulp-rename');
var connect = require('gulp-connect');
var watch = require('gulp-watch');
var copy = require('gulp-contrib-copy');
var uglify = require('gulp-uglify');
var version = generateMixed(11);

gulp.task('clean', function() {
    return gulp.src(['dest/'], {
            read: false
        })
        .pipe(clean());
});

gulp.task('reversion', function(){
    fs.writeFile('version', version, function(err){
        if(err) return console.log('写入错误');
        console.log('更换版本成功');
    });
});

gulp.task('readversion', function(){
    version = fs.readFileSync('version', 'utf-8');
});

gulp.task('ejsLess', function() {
    return gulp.src('./res/less/lib.ejs')
        .pipe(ejs({
            version: version
        }))
        .pipe(rename({
            extname: '.less'
        }))
        .pipe(gulp.dest('./res/less/'));
});

gulp.task('html', function() {
    return gulp.src(['./res/html/*.html', './res/*.html'])
        .pipe(ejs({
            version: version
        }))
        .pipe(htmlmin({
            collapseWhitespace: true,
            removeComments: true
        }))
        .pipe(gulp.dest('./dest/'))
        .pipe(connect.reload());
});

gulp.task('less', function() {
    return gulp.src('./res/less/style.less')
        .pipe(less({
            plugins: [cleancss],
            paths: [path.join(__dirname, 'less', 'includes')]
        }))
        .pipe(minifyCSS())
        .pipe(gulp.dest('./dest/css/'))
        .pipe(connect.reload());
});

gulp.task('copyImg', function() {
    gulp.src('./res/images/*')
        .pipe(gulp.dest('./dest/images/'))
        .pipe(connect.reload());
        
    gulp.src('./res/timages/*')
        .pipe(gulp.dest('./dest/timages/'))
        .pipe(connect.reload());

    gulp.src('./res/css/*')
        .pipe(gulp.dest('./dest/css/'));
});

gulp.task('copyScript', function() {
    gulp.src('./res/js/*.js')
        .pipe(gulp.dest('./dest/js/'))
        .pipe(connect.reload());
});

gulp.task('connect', function() {
    connect.server({
        root: 'dest/',
        livereload: true
    });
});

gulp.task('watch', function() {
    gulp.watch('./res/less/*.less', ['less']);
    gulp.watch(['./res/js/*.js'], ['copyScript']);
    gulp.watch(['./res/html/*.html', './res/html/common/*'], ['html']);
    gulp.watch(['./res/images/*', './res/timages/*'], ['copyImg']);
});
gulp.task('default', ['clean', 'reversion'], function() {
    // 将你的默认的任务代码放在这
    gulp.start(['html', 'less', 'copyImg', 'copyScript', 'watch', 'connect']);
});

function generateMixed(n) {
    var chars = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','v','y','z'];
     var res = '';
     for(var i = 0; i < n ; i ++) {
         var id = Math.ceil(Math.random()*35);
         res += chars[id];
     }
     return res;
}